package com.leonzx.base.controller.information;

import com.leonzx.base.config.pager.Page;
import com.leonzx.base.config.pager.PageInfo;
import com.leonzx.base.controller.base.BaseController;
import com.leonzx.base.entity.system.FileOnline;
import com.leonzx.base.entity.system.Information;
import com.leonzx.base.entity.system.User;
import com.leonzx.base.service.system.InformationService;
import com.leonzx.base.service.system.UserService;
import com.leonzx.base.utils.ShortMessageUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.formula.functions.T;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import static org.jboss.logging.MDC.getMap;

@RestController
@RequestMapping("/ajax/information")
public class AjaxInformationController extends BaseController {
    @Autowired
    private InformationService informationService;
    @Autowired
    private UserService userService;
    /**
     * 短信列表
     *
     * @param information
     * @param page
     * @return
     */
    @RequestMapping("/list")
    public Map<String, Object> list(Information information, @PageInfo(pageSize = Page.PAGE_SIZE, orderBy = "create_time") Page<Information> page) {
        Map<String, Object> result = getMap();
        try {
            List<Information> baseInformations = informationService.getAllBaseInformation(information);
            page.setResult(baseInformations);
            SimpleDateFormat sdf = new SimpleDateFormat("yyy-MM-dd HH:mm:ss");
            //循环list，处理数据
            for (Information b : baseInformations) {
                String createTimeStr = "";
                if (b.getCreateTime() != null) {
                    createTimeStr = sdf.format(b.getCreateTime());
                }
                String createUserName = userService.getUserNameById(b.getCreateUserId());
                b.setCreateTimeStr(createTimeStr);
                b.setCreateUserName(createUserName);
            }
            result.put("count", page.getTotalCount());
            result.put("data", baseInformations);
            result.put("code", "0");
            result.put("msg", "加载完成");
        } catch (Exception e) {
            logger.error("获取在线文件数据失败", e);
            addFailMessage(result, "获取在线文件数据失败");
        }
        return result;
    }


    /**
     * 添加短信或者更新信息
     *
     * @param information
     * @return
     */
    @RequestMapping("/save")
    @RequiresPermissions("/information/list")
    public Map<String, Object> saveorupdate(Information information) {
        Map<String, Object> result = getMap();
        try {
            if (StringUtils.isNotBlank(information.getId())) {
                informationService.update(information);
                addSuccessMessage(result, "短信更新成功");
            } else {
                information.setCreateUserId(currentUser().getId());
                informationService.insert(information);
                addSuccessMessage(result, "保存短信成功");
            }
        } catch (Exception e) {
            logger.error("保存短信失败", e);
            addFailMessage(result, "保存短信失败");
        }
        return result;
    }
    /**
     * 发生短信
     *
     * @param id
     * @param phoneNumber
     * @param content
     * @return
     */
    @RequestMapping("/sendContent")
    public Map<String, Object> sendContent(String id ,String phoneNumber,String content) {
        Map<String, Object> result = getMap();
        try {
            ShortMessageUtil.sendContent(phoneNumber, content);
        } catch (Exception e) {
            logger.error("发送短信失败", e);
            addFailMessage(result, "发送短信失败");
        }
        return result;
    }
}
